class Solution {
    bool isSymmetric(TreeNode* root)
    {
        if (!root) {
            return true;
        }
        return isSameTree(root->left, invertTree(root->right));
    }

    bool isSameTree(TreeNode* p, TreeNode* q)
    {
        if ((p && q)) {
            return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
        } else
            return !p && !q;
    }

    TreeNode* invertTree(TreeNode* root)
    {
        TreeNode* p;
        if (root == NULL) {
            return NULL;
        }
        p = root->left;
        root->left = root->right;
        root->right = p;
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
}